package com.edushare.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.edushare.entity.Group;
import com.edushare.entity.User;

import android.annotation.SuppressLint;
import android.util.Log;

@SuppressLint("SimpleDateFormat")
public class EntityController {
	private JSONController jController = new JSONController();
	private static String retrieve_open_event_url = "http://unifiq.5kportal.com/retrieve_open_event.php";
	private static String retrieve_open_event_by_id_url = "http://unifiq.5kportal.com/retrieve_open_event_by_id.php";
	private static String create_event_url = "http://unifiq.5kportal.com/create_event.php";
	private static String retrieve_event_url = "http://unifiq.5kportal.com/retrieve_event.php";
	private static String retrieve_group_url = "http://unifiq.5kportal.com/retrieve_group.php";
	private static String retrieve_user_group_id_url = "http://unifiq.5kportal.com/retrieve_user_group_id.php";
	private static String retrieve_group_user_id_url = "http://unifiq.5kportal.com/retrieve_group_user_id.php";
	private static String retrieve_checkin_url = "http://unifiq.5kportal.com/retrieve_checkin.php";
	private static String create_checkin_url = "http://unifiq.5kportal.com/create_checkin.php";
	private static String retrieve_open_event_attendance_url = "http://unifiq.5kportal.com/retrieve_open_event_attendance.php";
	private static String create_open_event_attendance_url = "http://unifiq.5kportal.com/create_open_event_attendance.php";
	private static String retrieve_notification_url = "http://unifiq.5kportal.com/retrieve_notification.php";
	private static String retrieve_user_url = "http://unifiq.5kportal.com/retrieve_user.php";
	private static String create_user_group_url = "http://unifiq.5kportal.com/create_user_group.php";
	private static String retrieve_user_by_id_url = "http://unifiq.5kportal.com/retrieve_user_by_id.php";
	private static String update_event_url = "http://unifiq.5kportal.com/update_event.php";
	private static String retrieve_avatar_url = "http://unifiq.5kportal.com/retrieve_avatar.php";
	private static String retrieve_avatar_by_id_url = "http://unifiq.5kportal.com/retrieve_avatar_by_id.php";
	private static String update_user_avatar_url = "http://unifiq.5kportal.com/update_user_avatar.php";
	private static String create_notification_url = "http://unifiq.5kportal.com/create_notification.php";
	private static String create_user_notification_url = "http://unifiq.5kportal.com/create_user_notification.php";
	private static String retrieve_user_notification_url = "http://unifiq.5kportal.com/retrieve_user_notification.php";
	private static String update_user_notification_url = "http://unifiq.5kportal.com/update_user_notification.php";
	private static String retrieve_avatar_filter_url = "http://unifiq.5kportal.com/retrieve_avatar_filter.php";

	private static final String TAG_SUCCESS = "success", TAG_OPEN_EVENT = "oe",
			TAG_ID = "id", TAG_NAME = "name", TAG_DATE_TIME = "dateTime",
			TAG_LOCATION = "location", TAG_CREATION_DATE = "creationDate",
			TAG_CREATOR_ID = "creatorID", TAG_LATITUDE = "latitude",
			TAG_LONGITUDE = "longitude", TAG_GROUP_ID = "groupID",
			TAG_EVENT = "event", TAG_GROUP = "group",
			TAG_IMAGE_PATH = "imagePath", TAG_USER_ID = "userID",
			TAG_GROUP_USERS = "groupUsers",
			TAG_LOCATION_REF = "locationReference", TAG_TYPE = "type",
			TAG_DESC = "description", TAG_CONDITION = "condition",
			TAG_EVENT_ID = "eventID", TAG_CHECKIN = "checkin",
			TAG_ADMIN_ID = "adminID", TAG_TYPE_ID = "typeID",
			TAG_CAPACITY = "capacity", TAG_SUBMISSION_DATE = "submissionDate",
			TAG_OPEN_EVENT_ID = "openEventID",
			TAG_OPEN_EVENT_ATTENDANCE = "oeAttendance",
			TAG_NOTIFICATION = "notification", TAG_UNIQUE_ID = "unique_id",
			TAG_USER = "user", TAG_UID = "uid", TAG_CATEGORY_ID = "categoryID",
			TAG_ICON_PATH = "iconPath", TAG_AVATAR = "avatar",
			TAG_AVATAR_ID = "avatarID", TAG_ORIGINAL = "original",
			TAG_UPDATE = "update", TAG_NOTIFICATION_ID = "notificationID",
			TAG_READ = "read", TAG_USER_NOTIFICATION = "userNotification",
			TAG_REQUIREMENT = "requirement", TAG_EMAIL = "email",
			TAG_BIRTHDAY = "birthday", TAG_SCHOOL = "school", TAG_PROFILEPIC = "profilePic";

	private final static SimpleDateFormat DATE_TIME_SDF = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat DATE_SDF = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static final SimpleDateFormat TIME_SDF = new SimpleDateFormat(
			"HH:mm");

	public static String convertDateTimeToFullString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy @ h:mm a");
		return sdf.format(date);
	}

	public static String convertDateTimeToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
		return sdf.format(date);
	}

	public static String dateTimeToString(Date date) {
		return DATE_TIME_SDF.format(date);
	}

	public static Date dateTimeToDate(Date date) {
		try {
			return DATE_SDF.parse(DATE_SDF.format(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static Date dateTimeToTime(Date time) {
		try {
			return TIME_SDF.parse(TIME_SDF.format(time));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String dateToString(Date date) {
		return DATE_SDF.format(date);
	}

	public static Date stringToDate(String date) throws ParseException {
		return DATE_SDF.parse(date);
	}

	public static Date stringToDateTime(String date) throws ParseException {
		return DATE_TIME_SDF.parse(date);
	}

	public static Date stringToTime(String time) throws ParseException {
		return TIME_SDF.parse(time);
	}

	public static String timeToString(Date time) {
		return TIME_SDF.format(time);
	}

	public boolean createEvent(int creatorID, String name, Date date,
			String time, int groupID, String locationRef, String desc) {

		String dateTime = dateToString(date) + " " + time;
		List<NameValuePair> eventParams = new ArrayList<NameValuePair>();
		eventParams.add(new BasicNameValuePair(TAG_CREATION_DATE,
				dateToString(Calendar.getInstance().getTime())));
		eventParams.add(new BasicNameValuePair(TAG_CREATOR_ID, String
				.valueOf(creatorID)));
		eventParams.add(new BasicNameValuePair(TAG_NAME, name));
		eventParams.add(new BasicNameValuePair(TAG_DATE_TIME, dateTime));
		eventParams.add(new BasicNameValuePair(TAG_GROUP_ID, String
				.valueOf(groupID)));
		eventParams.add(new BasicNameValuePair(TAG_LOCATION_REF, locationRef));
		eventParams.add(new BasicNameValuePair(TAG_DESC, desc));

		JSONObject json = jController.makeHttpRequest(create_event_url, "POST",
				eventParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}



	public ArrayList<Group> retrieveGroup(ArrayList<Integer> grpIDArr) {
		ArrayList<Group> grpArr = new ArrayList<Group>();

		for (int a = 0; a < grpIDArr.size(); a++) {
			grpArr.add(retrieveGroup(grpIDArr.get(a)));
		}

		return grpArr;
	}

	public Group retrieveGroup(int grpID) {
		Group grp = new Group();

		JSONArray grpJArr = null;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_GROUP_ID, String.valueOf(grpID)));
		JSONObject json = jController.makeHttpRequest(retrieve_group_url,
				"GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				grpJArr = json.getJSONArray(TAG_GROUP);

				for (int i = 0; i < grpJArr.length(); i++) {
					JSONObject jObject = grpJArr.getJSONObject(i);

					int id = jObject.getInt(TAG_GROUP_ID);
					String name = jObject.getString(TAG_NAME);
					String type = jObject.getString(TAG_TYPE);
					String desc = jObject.getString(TAG_DESC);
					String url = jObject.getString(TAG_IMAGE_PATH);

					grp.setGroupID(id);
					grp.setName(name);
					grp.setDescription(desc);
					grp.setImagePath(url);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return grp;
	}

	public ArrayList<Integer> retrieveUserGroupID(int userID) {
		ArrayList<Integer> grpIDArr = new ArrayList<Integer>();

		JSONArray grpJArr = null;
		List<NameValuePair> groupUsersParams = new ArrayList<NameValuePair>();
		groupUsersParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_user_group_id_url, "GET", groupUsersParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				grpJArr = groupUsersJSon.getJSONArray(TAG_GROUP_USERS);

				for (int i = 0; i < grpJArr.length(); i++) {
					JSONObject jObject = grpJArr.getJSONObject(i);
					grpIDArr.add(jObject.getInt(TAG_GROUP_ID));

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return grpIDArr;
	}

	public ArrayList<Integer> retrieveGroupUserID(int groupID) {
		ArrayList<Integer> userIDArr = new ArrayList<Integer>();

		JSONArray grpJArr = null;
		List<NameValuePair> groupUsersParams = new ArrayList<NameValuePair>();
		groupUsersParams.add(new BasicNameValuePair(TAG_GROUP_ID, String
				.valueOf(groupID)));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_group_user_id_url, "GET", groupUsersParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				grpJArr = groupUsersJSon.getJSONArray(TAG_GROUP_USERS);

				for (int i = 0; i < grpJArr.length(); i++) {
					JSONObject jObject = grpJArr.getJSONObject(i);
					userIDArr.add(jObject.getInt(TAG_USER_ID));

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return userIDArr;
	}

	public boolean createCheckin(int userID, int openEventID, int eventID) {

		List<NameValuePair> checkinParams = new ArrayList<NameValuePair>();
		checkinParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		checkinParams.add(new BasicNameValuePair(TAG_OPEN_EVENT_ID, String
				.valueOf(openEventID)));
		checkinParams.add(new BasicNameValuePair(TAG_EVENT_ID, String
				.valueOf(eventID)));

		JSONObject json = jController.makeHttpRequest(create_checkin_url,
				"POST", checkinParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public ArrayList<ArrayList<Integer>> retrieveOpenEventAttendance(
			int openEventID, int userID) {
		ArrayList<ArrayList<Integer>> idArr = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> oeIDArr = new ArrayList<Integer>(), userIDArr = new ArrayList<Integer>();
		JSONArray openEventAttendanceJArr = null;

		String condition = "";

		if (openEventID != 0) {
			condition += "openEventID = " + String.valueOf(openEventID);
			if (userID != 0) {
				condition += " AND ";
			}
		}

		if (userID != 0) {
			condition += "userID = " + String.valueOf(userID);
		}

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add((new BasicNameValuePair(TAG_CONDITION, "WHERE " + condition)));
		JSONObject json = jController.makeHttpRequest(
				retrieve_open_event_attendance_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				openEventAttendanceJArr = json
						.getJSONArray(TAG_OPEN_EVENT_ATTENDANCE);

				for (int i = 0; i < openEventAttendanceJArr.length(); i++) {
					JSONObject jObject = openEventAttendanceJArr
							.getJSONObject(i);

					oeIDArr.add(jObject.getInt(TAG_OPEN_EVENT_ID));
					userIDArr.add(jObject.getInt(TAG_USER_ID));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		idArr.add(oeIDArr);
		idArr.add(userIDArr);
		return idArr;
	}



	public User retrieveUser(String uniqueID) {
		User user = new User();

		JSONArray userJArr = null;
		List<NameValuePair> userParams = new ArrayList<NameValuePair>();
		userParams.add(new BasicNameValuePair(TAG_UNIQUE_ID, uniqueID));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_user_url, "GET", userParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				userJArr = groupUsersJSon.getJSONArray(TAG_USER);

				for (int i = 0; i < userJArr.length(); i++) {
					JSONObject jObject = userJArr.getJSONObject(i);

					user.setId(jObject.getInt(TAG_UID));
					user.setUniqueID(jObject.getString(TAG_UNIQUE_ID));
					user.setName(jObject.getString(TAG_NAME));
					user.setAvatarID(jObject.getInt(TAG_AVATAR_ID));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return user;
	}

	public User retrieveUser(int userID) {
		User user = new User();

		JSONArray userJArr = null;
		List<NameValuePair> userParams = new ArrayList<NameValuePair>();
		userParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_user_by_id_url, "GET", userParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				userJArr = groupUsersJSon.getJSONArray(TAG_USER);

				for (int i = 0; i < userJArr.length(); i++) {
					JSONObject jObject = userJArr.getJSONObject(i);

					user.setId(jObject.getInt(TAG_UID));
					user.setUniqueID(jObject.getString(TAG_UNIQUE_ID));
					user.setName(jObject.getString(TAG_NAME));
					user.setAvatarID(jObject.getInt(TAG_AVATAR_ID));
					user.setBirthday(jObject.getString(TAG_BIRTHDAY));
					user.setEmail(jObject.getString(TAG_EMAIL));
					user.setSchool(jObject.getString(TAG_SCHOOL));
					user.setProfilePic(jObject.getString(TAG_PROFILEPIC));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return user;
	}

	public boolean createUserGroup(int userID, int groupID) {

		List<NameValuePair> userGroupParams = new ArrayList<NameValuePair>();
		userGroupParams.add(new BasicNameValuePair(TAG_GROUP_ID, String
				.valueOf(groupID)));
		userGroupParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));

		JSONObject json = jController.makeHttpRequest(create_user_group_url,
				"POST", userGroupParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	

}
